package com.poetize.plus.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.poetize.plus.entity.dto.NoticeDTO;
import com.poetize.plus.entity.dto.page.NoticePageDTO;
import com.poetize.plus.entity.vo.NoticeVO;
import com.poetize.plus.entity.vo.page.NoticePageVO;
import com.poetize.plus.service.NoticeService;
import com.poetize.plus.utils.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author zuosy
 * @Date 2024/3/30 14:39
 **/
@Tag(name = "公告信息")
@RestController
@RequestMapping("/admin/notice")
@RequiredArgsConstructor
public class NoticeController {

    private final NoticeService noticeService;

    @GetMapping("/noticePage")
    @Operation(summary = "公告分页数据")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Page<NoticePageVO>> noticePage(NoticePageDTO noticePageDTO){
        return R.ok(noticeService.noticePage(noticePageDTO));
    }

    @PostMapping("/addNotice")
    @Operation(summary = "新增公告信息")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> addNotice(@RequestBody @Validated(Insert.class) NoticeDTO noticeDTO){
        return R.ok(noticeService.addNotice(noticeDTO));
    }

    @GetMapping("/{id}")
    @Operation(summary = "公告详情")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<NoticeVO> queryNotice(@PathVariable("id") Long id){
        return R.ok(noticeService.queryNotice(id));
    }

    @PostMapping("/updateNotice")
    @Operation(summary = "修改公告信息")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> updateNotice(@RequestBody @Validated(Update.class) NoticeDTO noticeDTO){
        return R.ok(noticeService.updateNotice(noticeDTO));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除公告信息")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> deleteNotice(@PathVariable("id") Long id){
        return R.ok(noticeService.deleteNotice(id));
    }

    @GetMapping("/{id}/{status}")
    @Operation(summary = "修改公告状态")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> enable(@PathVariable("id")Long id,
                             @PathVariable("status") Boolean status){
        return R.ok(noticeService.enable(id, status));
    }
}
